package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.pojo.dto.WaterMarkDTO;
import com.xbongbong.paas.pojo.vo.WaterMarkVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisConstant;
import com.xbongbong.paas.service.CompanyConfigService;
import com.xbongbong.paas.service.WaterMarkService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.menu.enums.OtherSetEnum;
import com.xbongbong.pro.menu.pojo.vo.PaasAppMenuVO;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyModel;
import com.xbongbong.sys.model.impl.CompanyConfigModelImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 水印实现层
 * @Author: 肖云涛
 * @Date: 2022/8/28 21:59
 */
@Service("waterMarkService")
public class WaterMarkServiceImpl implements WaterMarkService {

    @Resource
    private CompanyConfigService companyConfigService;

    @Resource
    private PaasRedisHelper paasRedisHelper;

    @Resource
    private CompanyModel companyModel;

    @Resource
    private CompanyConfigModelImpl companyConfigModel;

    @Override
    public WaterMarkVO getWaterMark(String corpid) throws XbbException {
        String key = CompanyConfigEnum.WATER_MARK.getAlias();
        String filePrefix = RedisConstant.KEY_COMPANY_CONFIG + ":" + corpid;
        String configValue = paasRedisHelper.getValue(filePrefix, key);
        if (Objects.isNull(configValue)) {
            configValue = companyConfigService.getCompanyConfigSetValue(corpid, key);
            paasRedisHelper.setValue(filePrefix, key, configValue, TimeConstant.TEN_MINUTE_DURATION);
        }
        WaterMarkVO waterMarkVO = JSON.parseObject(configValue, WaterMarkVO.class);
        return waterMarkVO;
    }

    @Override
    public WaterMarkVO updataWaterMarkState(WaterMarkDTO waterMarkDTO) throws XbbException {
        String corpid = waterMarkDTO.getCorpid();
        WaterMarkVO waterMarkVO = new WaterMarkVO();
        String key = CompanyConfigEnum.WATER_MARK.getAlias();
        String filePrefix = RedisConstant.KEY_COMPANY_CONFIG + ":" + corpid;
        String corpName = companyModel.getByKey(corpid).getCorpName();
        BeanUtil.copyProperties(waterMarkDTO, waterMarkVO);
        waterMarkVO.setCorpName(corpName);
        String waterMarkvalue = JSONObject.toJSONString(waterMarkVO);
        CompanyConfigEntity entity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.WATER_MARK.getAlias(), corpid);
        if (Objects.isNull(entity)){
            companyConfigModel.insert(new CompanyConfigEntity(corpid, CompanyConfigEnum.WATER_MARK.getName(), CompanyConfigEnum.WATER_MARK.getAlias(), waterMarkvalue));
        }else {
            entity.setConfigValue(waterMarkvalue);
            companyConfigModel.update(entity);
        }
        paasRedisHelper.removeValue(filePrefix, key);
        return waterMarkVO;
    }

    @Override
    public PaasAppMenuVO getWaterMarkMenu() throws XbbException {
        PaasAppMenuVO paasAppMenuVO = new PaasAppMenuVO();
        paasAppMenuVO.setName(OtherSetEnum.GLOBAL_WATERMARK.getName());
        paasAppMenuVO.setAlias(OtherSetEnum.GLOBAL_WATERMARK.getAlias());
        paasAppMenuVO.setAlias("cloudCode_");
        paasAppMenuVO.setIcon(AccessLinkEnum.GLOBAL_WATERMARK.getIcon());
        paasAppMenuVO.setColor(AccessLinkEnum.GLOBAL_WATERMARK.getColor());
        paasAppMenuVO.setUrl(AccessLinkEnum.GLOBAL_WATERMARK.getUrl());
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("mobileLinkAppId","");
        valueMap.put("pcLinkAppId", AccessLinkEnum.GLOBAL_WATERMARK.getUrl());
        Map<String, Object> actionsMap = new HashMap<>();
        actionsMap.put("value",valueMap);
        actionsMap.put("type","openMenuQianKun");
        Map<String, Object> map  = new HashMap<>();
        map.put("actions",actionsMap);
        map.put("pcActions",new ArrayList<>());
        map.put("mobileActions",new ArrayList<>());
        Map<String, Object> keyMap = new HashMap<>();
        keyMap.put("key","value");
        map.put("diy_params",keyMap);
        String actions = JSONObject.toJSONString(map);
        paasAppMenuVO.setActions(actions);
        return paasAppMenuVO;
    }
}
